package com.lipi.mall.order.config;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lipi.mall.order.vo.PayVO;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000118601521";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC8CZAyPSbXLYQZ7rY8qW28NkcmYvi1tlGIQ2qBIXhKumtEfXyKDflgY1bTWTwiVbu80Ix4CzMe9YmCR7MW/09ec58+/lG3GiayrztBXW27I13/KewUDlVIUx2q6G7cu01BlToSfcx92nKbuWZJxu6gY3MP0+5GaLPOXMPsMusL7oUSN7lLrRmGeeZ9RqsWCKc0md/zCwdxnZQt6LifU/nfYx0a7aWlN4xYG6IHhgGIcT6+Du89hR6+sAmMaE315iiD2iAvnn+twVXl1YMlONIWwT1jWiBI1RnpOyIX0c0qmNYTHvlodY4+drihqXXA89PxK/+owQJntsQT+knExbHfAgMBAAECggEAdsIXdxNWJ4qQpwwzvoZHTxp8v0q8x48ZWVUyu9GWtqOcQcp7UHnY6T50RNFUAF9jTbHtNZflNoVezpBYFIs05nNhvRhEJb93d1zisLrAJfQFVrV0a4pN5HrR/vIDqpcFCQmlfq5C661+uslSSABDD7sfnXx9Pay8+vVtrAzQlFyAPRd4ZjcZYl0PYUOGuYeYq7o4LXAsOs9jcqWKl63K6x5xR9jgunMxCkC+QzFuGyxEDRgfhFQd291Oa5mf+qGrcOTQ1bnpQOt0emKRLEuUNK7pIJ1g0TpoQtrSE/v/+rxHPiWioyH2+cpyONazUIaGcCfS2i3oOLzmGt7IJgNDiQKBgQDb5wJ41R1vtjBkYXMhI27s+3DeDBCvHjhAJufFHupeNkgtBgFSj0+lYnV3VY6Wt3ntQG+oUdWc82hDWLhLfslsdFXof/aahLLipjjMgFlU1BRIkpNwGJglsfnBwuo4OBc0jbUE4LWBqnwd7sz7lweI7oHvyR3Y9otNifkpPphgrQKBgQDa53wlgUhidibM0HhHyx0uM21s8/U4Q2RAHi29qq2KHU+61X7tpp8A8iMsVab7A5GctArN5C9/H07C7KpztaZvaeH7imfYITpnaVqZciVVHmNR4HOnLFpC4urUxSkRZuFWJiq4z2R/0SUpovdq8ozozuhDgVmIvD3YePc00XBSOwKBgQCQEgx7j9ymSO3xhisApg9Rsfajc7ImmRzvYs3sRrH0IYot/UY2ie/7gcNF+MbflbuLJfFBjMPv/UTtAp5Tz/A1Xw69scCmQZRVi+Ogj+J3Ai970JRKF8Lux2jDemkrhgwmAUV8uaARINoKXl7r4mosW17D87bbY8OmfGPCkuEQTQKBgQCbiOBPsaUhuHtnCTR9JlcsbYPxZwE0IcYIPKkZWz0EpkDxKiKObtYJEQJmzNDhBy8d3VrT4a7RrwgCPa/6mPWRsfTMChIwoaNiuJuT7WOTMA03rrfd7wiO/7YEybWdVLEEygeO2mS44Hx8MfuD9KSkAedLl4trOPzkY0aaM7mP4wKBgFfAp8AIlU2ZbdUpIBKKZcLAUnoQ4jY+MyyIhSJb7ZAr5QIdCcAqfG79pzNiltEU3RhbX98JijyOYkQUJsLHfb+BF00Wl7+8vN6IhOdVbt/jtBDi+khYWRZkWyWzGTIXOIzYpx1OMfaKgGsW5w/09aq3EKJyq3vpkUePDGkUr8wN";


    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsoAedrsyARuj0iBkxd8jJa/ta6yjbmsMXjRSBDr60TJ+gyPXqKhkwHEeJO7+sfEIkJsu+2qJ3IlFHiO0RDe1HVOExgEQhJwpP4QrNhly7ibxqC+K9JvO4egwYS9KG+RRGVu5cd+z5jNqvlZQqYM7LSwTvQqROOLHoxuZzb5jg1UJ4UYM74yI1THrp6K1jbEWC4+tbPzKmbh2VSpEwL5jtZWGhdtzTvMBZyH9EZygWFfI6QY4/pPjwrHgCEQrfn85R2pKGnV6zAMifdYhT3lN/GtDbWuwFFQ8+knLbe/coJk4mb5Ir1p1U1j73N5HcC12YMbmXaoUSIBqHiWUy3Ic2QIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://ip3hbkinw4.cdhttp.cn/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.mall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVO vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
